package client;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import client.spells.Spell;

public class Player implements Serializable {

	private static final long serialVersionUID = 1L;
	private Point location;
	private int money;
	private List<Creature> creatures;
	private MagicColor defaultColor;
	private Area currentArea;
	private ManaPool manaPool;
	private Creature activeCreature;
	private Rectangle boundingRect;
	private Point previousLocation;

	// equipment
	// image
	// spells
	private Hashtable<Spell, Integer> spells;

	public Player() {
		money = 200;
		creatures = new ArrayList<Creature>();
		spells = new Hashtable<Spell, Integer>();
		location = new Point(200, 200);
		previousLocation = location;
		boundingRect = new Rectangle(0, 0, 22, 22);
		boundingRect.setLocation(location.x - 1, location.y - 1);
	}

	public void addCreature(Creature c) {
		if (creatures != null) {
			creatures.add(c);
		}
	}

	public List<Creature> getCreatures() {
		return creatures;
	}

	public int getX() {
		return location.x;
	}

	public int getY() {
		return location.y;
	}

	public void setX(int x) {
		location.x = x;
	}

	public void setY(int y) {
		location.y = y;
	}

	public Area getCurrentArea() {
		return currentArea;
	}

	public void setCurrentArea(Area currentArea) {
		this.currentArea = currentArea;
	}
	
	public void addSpell(Spell spell){
		spells.put(spell, 1);
	}

	public void move(int xOffset, int yOffset) {
		//check future location
		boundingRect.translate(xOffset, yOffset);
		if (currentArea.checkCollisions(boundingRect) == false) {
			previousLocation.x = location.x;
			previousLocation.y = location.y;
			location.x += xOffset;
			location.y += yOffset;
		} else {
			//a collision will occur, so put player back 
			location.x = previousLocation.x;
			location.y = previousLocation.y;
		}
		boundingRect.setLocation(location.x - 1, location.y - 1);
	}

	public void setDefaultColor(MagicColor color) {
		defaultColor = color;
	}

	public int getMoney() {
		return money;
	}

	public Creature getActiveCreature() {
		return activeCreature;
	}

	public void setActiveCreature(Creature activeCreature) {
		this.activeCreature = activeCreature;
	}

	public ManaPool getManaPool(){
		return manaPool;
	}
	
	public void paint(Graphics g) {

		g.setColor(defaultColor.getColor());
		g.fillOval(getX(), getY(), 20, 20);
	}
}
